<template>
  <div class="app-container">
    <!-- 类型-->
    <!--<div class="filter-container">-->
    <!--<el-tag-->
    <!--v-for="(tag,index) in tags"-->
    <!--:key="tag.name"-->
    <!--:class="bgindex === index?'active':''"-->
    <!--style="cursor: pointer;margin-left: 10px"-->

    <!--@click.native="tagclick(tag.cid,index)">-->
    <!--{{ tag.name }}-->
    <!--</el-tag>-->
    <!--</div>-->
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input size="mini" v-model="listQuery.tradNo" clearable class="filter-item" style="width: 200px;" placeholder="订单编号"/>
      <!--<el-input v-model="listQuery.companyName" clearable class="filter-item" style="width: 200px;" placeholder="企业名称"/>-->
      <!--<el-input v-model="listQuery.categoryName" clearable class="filter-item" style="width: 200px;" placeholder="分类名称"/>-->
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <!--<el-button v-permission="['POST /admin/order/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>-->
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button> -->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" size="mini" element-loading-text="正在查询中。。。" highlight-current-row>
      <!--<el-table-column align="center" label="ID" prop="id"/>-->
      <el-table-column align="center" label="订单编号" prop="tradNo" width="200px"/>

      <!--<el-table-column align="center" label="企业名称" prop="companyName"/>-->
      <!--<el-table-column align="center" label="行业大类名称" prop="categoryName"/>-->
      <el-table-column align="center" label="缴费人数" prop="payPeopleNum" width="100px"/>
      <!-- <el-table-column align="center" label="课时单价" prop="price"/> -->
      <el-table-column align="center" label="总金额" prop="payAmount"  width="100px"/>

      <el-table-column align="center" label="支付类型" prop="payType"width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payType===1" type="primary">微信</el-tag>
          <el-tag v-if="scope.row.payType===2" type="success">线下</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="支付情况" prop="payStatus" width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus===0" type="success">未支付</el-tag>
          <el-tag v-if="scope.row.payStatus===1" type="primary">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="缴费情况" prop="payStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.confirmPay===0" type="info">等待对方确认收款</el-tag>
          <el-tag v-if="scope.row.confirmPay===1" type="success">已确认收款</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="下单时间" prop="createDate"  width="200px"/>
      <!--<el-table-column align="center" label="支付类型" prop="payType">-->
      <!--<template slot-scope="scope">-->
      <!--<el-tag v-if="scope.row.payType===1" type="primary">微信</el-tag>-->
      <!--<el-tag v-if="scope.row.payType===2" type="success">转账</el-tag>-->
      <!--<el-tag v-if="scope.row.payType===3" type="info">线下</el-tag>-->
      <!--</template>-->
      <!--</el-table-column>-->

      <el-table-column align="center" label="发票" prop="invoiceUrl" width="100px">
        <template slot-scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="scope.row.invoiceUrl" style="width:300px;height:300px;">
            <img slot="reference" :src="scope.row.invoiceUrl" style="width:50px;heigth:50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="转账凭证" prop="payEvidence" width="100px">
        <template slot-scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="scope.row.payEvidence" style="width:300px;height:300px;">
            <img slot="reference" :src="scope.row.payEvidence" style="width:50px;heigth:50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button style="width:60%;" type="primary" size="mini" @click="getOrderStudentList(scope.row)">查询缴费成员</el-button>
          <!--<el-button v-permission="['POST /admin/order/update']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">

        <el-form-item label="上传发票" prop="invoiceUrl">
          <el-upload
            :headers="headers"
            :action="uploadPath"
            :show-file-list="false"
            :on-success="uploadPicUrl"
            class="avatar-uploader"
            accept=".jpg,.jpeg,.png,.gif">
            <img v-if="dataForm.invoiceUrl" :src="dataForm.invoiceUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <!--<el-form-item label="上传凭证" prop="payEvidence">-->
        <!--<el-upload-->
        <!--:headers="headers"-->
        <!--:action="uploadPath"-->
        <!--:show-file-list="false"-->
        <!--:on-success="uploadPicUrl1"-->
        <!--class="avatar-uploader"-->
        <!--accept=".jpg,.jpeg,.png,.gif">-->
        <!--<img v-if="dataForm.payEvidence" :src="dataForm.payEvidence" class="avatar">-->
        <!--<i v-else class="el-icon-plus avatar-uploader-icon"/>-->
        <!--</el-upload>-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取消</el-button>
        <el-button size="mini" v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

    <!-- 订单缴费学员结果 -->
    <el-dialog :visible.sync="dialogFormVisibleOrderStudent" :close-on-click-modal="false" title="缴费学员" width="90%">
      <!--<div class="filter-container">-->
      <!--<el-input v-model="listQueryLoadingOrderStudent.id" clearable class="filter-item" style="width: 200px;" placeholder="请输入活动类目Id"/>-->
      <!--<el-input v-model="listQueryLoadingOrderStudent.title" clearable class="filter-item" style="width: 200px;" placeholder="请输入活动类目标题"/>-->
      <!--<el-button v-permission="['GET /admin/listCompanyOrderStudent/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilterActivityCategory">查找</el-button>-->
      <!--&lt;!&ndash; <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button> &ndash;&gt;-->
      <!--</div>-->
      <el-table v-loading="listLoadingOrderStudent" :data="listOrderStudent" size="small" element-loading-text="正在查询中。。。" border fit highlight-current-row>

        <el-table-column align="center" label="ID" prop="id" sortable/>
        <el-table-column align="center" label="订单id" prop="ordersId"/>
        <el-table-column align="center" label="学生ID" prop="studentId"/>
        <el-table-column align="center" label="学生名字" prop="studentName"/>
        <el-table-column align="center" label="创建时间" prop="createDate" />
      </el-table>
      <pagination v-show="totalOrderStudent>0" :total="totalOrderStudent" :page.sync="listQueryLoadingOrderStudent.page" :limit.sync="listQueryLoadingOrderStudent.limit" @pagination="getListOrderStudentList" />
    </el-dialog>

  </div>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 145px;
    height: 145px;
    display: block;
  }
</style>

<script>
import { listCompanyOrder, listCompanyOrderStudent } from '@/api/companyOperate'
import { uploadPath } from '@/api/storage'
import { getToken } from '@/utils/auth'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'CompanyOrder',
  components: { Pagination },
  data() {
    return {
      uploadPath,
      updateParam: undefined,
      list: undefined,
      total: 0,
      listLoading: true,
      listQuery: {
        id: undefined,
        page: 1,
        limit: 10,
        companyId: undefined,
        tradNo: undefined,
        payType: undefined,
        companyName: undefined,
        categoryName: undefined,
        sort: 'id',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        tradNo: undefined,
        companyId: undefined,
        companyName: undefined,
        categoryId: undefined,
        categoryName: undefined,
        payPeopleNum: undefined,
        payAmount: undefined,
        payType: undefined,
        payStatus: undefined,
        payEvidence: undefined,
        invoiceUrl: undefined,
        confirmPay: undefined,
        createDate: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        name: [
          { required: true, message: '商家名称不能为空', trigger: 'blur' }
        ]
      },
      downloadLoading: false,
      tags: [
        { name: '全部', cid: '' },
        { name: '微信', cid: 1 },
        { name: '转账', cid: 2 },
        { name: '线下', cid: 3 }
      ],
      bgindex: -1,

      /* 订单缴费学员参数*/
      listOrderStudent: undefined,
      totalOrderStudent: 0,
      listLoadingOrderStudent: true,
      listQueryLoadingOrderStudent: {
        page: 1,
        limit: 20,
        id: undefined,
        ordersId: undefined,
        sort: 'create_date',
        order: 'desc'
      },
      dataFormActivityCategory: {
        id: undefined,
        ordersId: undefined,
        studentId: undefined,
        studentName: undefined,
        create_date: undefined
      },
      dialogFormVisibleOrderStudent: false
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 标签类事件
    tagclick(cid, index) {
      if (this.bgindex === index) {
        this.bgindex = -1
        this.listQuery.payType = ''
        this.getList()
      } else {
        this.bgindex = index
        this.listQuery.payType = cid
        this.getList()
      }
    },
    // 初始化参数
    init: function() {
      this.getList()
    },
    getList() {
      this.listLoading = true
      listCompanyOrder(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.completeInfo = response.data.data.object
          this.total = response.data.data.total
          this.listLoading = false
          this.completeInfoFun()
        })
        .catch(() => {
          this.list = []
          this.completeInfo = 0 // 未完善
          this.total = 0
          this.listLoading = false
        })
    },
    completeInfoFun() {
      if (this.completeInfo === 0) {
        this.$confirm('您的信息未完善, 是否跳转完善信息页面', '提示', {
          showClose: false,
          showCancelButton: false,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.getCompleteInfo()
          this.$message({
            type: 'success',
            message: '跳转完善信息页面'
          })
        }).catch(() => {
          this.getCompleteInfo()
          // this.$message({
          //
          //   type: 'info',
          //   message: '跳转失败'
          // })
        })
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        tradNo: undefined,
        companyId: undefined,
        companyName: undefined,
        categoryId: undefined,
        categoryName: undefined,
        payPeopleNum: undefined,
        payAmount: undefined,
        payType: undefined,
        payStatus: undefined,
        payEvidence: undefined,
        invoiceUrl: undefined,
        confirmPay: undefined,
        createDate: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    uploadPicUrl: function(response) {
      this.dataForm.invoiceUrl = response.data.url
    },
    uploadPicUrl1: function(response) {
      this.dataForm.payEvidence = response.data.url
    },

    /* 查询订单缴费学员*/
    getOrderStudentList(row) {
      this.dialogFormVisibleOrderStudent = true
      this.listQueryLoadingOrderStudent.ordersId = row.id
      this.dataFormActivityCategory.ordersId = row.id
      this.getListOrderStudentList()
    },
    getListOrderStudentList() {
      this.listLoadingOrderStudent = true
      listCompanyOrderStudent(this.listQueryLoadingOrderStudent)
        .then(response => {
          this.listOrderStudent = response.data.data.items
          this.totalOrderStudent = response.data.data.total
          this.listLoadingOrderStudent = false
        })
        .catch(() => {
          this.listOrderStudent = []
          this.totalOrderStudent = 0
          this.listLoadingOrderStudent = false
        })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          listCompanyOrderStudent(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.message
              })
            })
        }
      })
    },
    getCompleteInfo() {
      this.$router.push({ path: '/companyOperateManage/completeInfo' })
    }

  }
}
</script>
